import { validatenull } from './validate';

const hasOwnProperty = Object.prototype.hasOwnProperty;
// 是否为属性
export function hasOwn(obj, key) {
    return hasOwnProperty.call(obj, key);
};

/**
 * 对象深拷贝
 */
export const deepClone = data => {
    var type = getObjType(data);
    var obj;
    if (type === 'array') obj = [];
    else if (type === 'object') obj = {};
    else return data;
    if (type === 'array') {
        for (var i = 0, len = data.length; i < len; i++) {
            data[i] = (() => {
                if (data[i] === 0) {
                    return data[i];
                }
                return data[i];
            })();
            if (data[i]) {
                delete data[i].$parent;
            }
            obj.push(deepClone(data[i]));
        }
    } else if (type === 'object') {
        for (var key in data) {
            if (data) {
                delete data.$parent;
            }
            obj[key] = deepClone(data[key]);
        }
    }
    return obj;
};

/**
 * 验证是否存在true/false
 */
export const vaildData = (val, def) => {
    if (typeof val === 'boolean') {
        return val;
    }
    return !validatenull(val) ? val : def;
};